package io.github.harmonly.skyblock.command;

import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.CommandPermission;
import dev.jorel.commandapi.arguments.PlayerArgument;
import io.github.harmonly.skyblock.SkyBlock;
import io.github.harmonly.skyblock.manager.DataManager;
import io.github.harmonly.skyblock.manager.ResourcesManager;
import io.github.harmonly.skyblock.util.Replacer;
import io.github.harmonly.skyblock.util.StableObject;
import net.wesjd.anvilgui.AnvilGUI;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.List;

public class PrefixCommand implements SBCommand {

    @Override
    public String name() {
        return "prefix";
    }

    @Override
    public void register() {
        new CommandAPICommand("prefix")
                .withPermission(CommandPermission.OP)
                .withArguments(new PlayerArgument("target"))
                .executesPlayer((sender, args) -> {
                    Player player = (Player) args.get(0);
                    if (player == null) {
                        return;
                    }
                    new AnvilGUI.Builder()
                            .plugin(SkyBlock.getInstance())
                            .title("Change " + player.getName() + "'s prefix")
                            .text("Enter prefix")
                            .onClick((slot, stateSnapshot) -> {
                                if (slot != AnvilGUI.Slot.OUTPUT) {
                                    return Collections.emptyList();
                                }
                                DataManager.getPlayerData(player).ifPresent(data -> {
                                    String prefix = ChatColor.translateAlternateColorCodes('&', stateSnapshot.getText());
                                    if (prefix.isEmpty()) {
                                        data.removeProperty("prefix");
                                    } else {
                                        data.addProperty("prefix", prefix);
                                        data.updateDisplay();

                                        StableObject message = ResourcesManager.getJson("message");
                                        sender.sendMessage(Replacer.replaceIn(
                                                message.getStringProperty("command.prefix.send.success", null),
                                                "player", player.getName(),
                                                "prefix", prefix));
                                        player.sendMessage(Replacer.replaceIn(
                                                message.getStringProperty("command.prefix.receive.success", null),
                                                "prefix", prefix)
                                        );
                                    }
                                });
                                return List.of(AnvilGUI.ResponseAction.close());
                            })
                            .open(sender);
                })
                .register();
    }

    @Override
    public void unregister() {
        CommandAPI.unregister("prefix");
    }
}
